<template>
  <view class="container">
    <uv-navbar
      bg-color="none"
      left-icon="scan"
      leftIconSize="50rpx"
      @leftClick="onScan"
    ></uv-navbar>
    <uv-swiper :list="list3" circular></uv-swiper>
    <view class="list">
      <view class="list-item" v-for="(item, index) in dataLits" :key="item.id">
        <view class="left-image" style="width: 25%">
          <span style="font-size: 32rpx">¥</span>
          <span style="font-size: 49rpx; margin-left: 12rpx">{{
            item.reduceMoney || item.discountMoney
          }}</span>
        </view>
        <view class="right" v-if="item.couponType == 1" style="width: 50%">
          <view class="title"
            >满{{ item.satisfyMoney }}减{{ item.reduceMoney }}</view
          >
          <view class="date">领取后{{ item.validDays }}天有效</view>
          <view class="date">使用范围：不限</view>
        </view>
        <view class="right" v-else style="width: 50%">
          <view class="title">无门槛劵</view>
          <view class="date">领取后{{ item.validDays }}有效</view>
          <view class="date">使用范围：不限</view>
        </view>
        <uv-button
          color="rgb(70, 53, 255)"
          text="领取"
          @click="onGetCoupon(item)"
        ></uv-button>
      </view>
    </view>
  </view>
</template>

<script>
import newHome from "@/components/newHome/newHome.vue";
import { toast } from "@/utils/common";
import {
  getMerchantInfo,
  getUserInfo,
  getCouponList,
  getCoupon,
} from "@/common/api.js";
const app = getApp();
export default {
  components: {
    newHome,
  },
  data() {
    return {
      loading: false,
      dataLits: [],
      list3: [
        "http://jy-nfc.oss-cn-hangzhou.aliyuncs.com/2024/12/30/98eb8797246548ca887f4ae214b77199.jpg",
        "http://jy-nfc.oss-cn-hangzhou.aliyuncs.com/2024/12/30/98eb8797246548ca887f4ae214b77199.jpg",
        "http://jy-nfc.oss-cn-hangzhou.aliyuncs.com/2024/12/30/98eb8797246548ca887f4ae214b77199.jpg",
      ],
      list: [
        {
          name: "关注",
        },
        {
          name: "推荐",
        },
        {
          name: "电影",
        },
        {
          name: "科技",
        },
      ],
    };
  },
  onPullDownRefresh: function () {
    this.getDate().then(() => {
      uni.stopPullDownRefresh();
    });
  },
  onLoad() {
    this.loading = true;
    this.getDate();
  },
  onShow() {
    uni.$once("update", () => {
      this.getUserDate();
    });
  },
  onHide() {},

  methods: {
    goPage(url) {
      const token = this.$store.state.user.token;
      if (!token) {
        app.navigationTo("page_user/login/login");
        return false;
      }
      app.navigationTo(url);
    },
    recharge(url) {
      toast("暂未开放");
    },
    getDate() {
      return new Promise((resolve, reject) => {
        getCouponList()
          .then((res) => {
            console.log(res);
            this.dataLits = res.rows;
            this.loading = false;
            resolve();
          })
          .catch((err) => {
            this.loading = false;
            console.log("userInfoerr", err);
          });
      });
    },
    getUserDate() {
      let api =
        this.$store.state.user.flag == 1 ? getMerchantInfo : getUserInfo;
      let prom =
        this.$store.state.user.flag == 1 ? null : this.$store.state.user.userId;
      return new Promise((resolve, reject) => {
        api(prom)
          .then((res) => {
            let data = res.data;
            data.merchantLogo = data.imgUrl || null;
            data.merchantName = data.nickName;
            this.merchantInfo = data;
            this.$store.commit("SET_WX_MINFO", this.merchantInfo);
            resolve();
          })
          .catch((err) => {
            console.log("userInfoerr", err);
          });
      });
    },
    onGetCoupon(item) {
      console.log(item);
      getCoupon({
        couponId: item.id,
        userId: this.$store.state.user.userId,
      })
        .then((res) => {
          console.log(res);
          uni.showToast({
            icon: "success",
            title: "领取成功",
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onScan() {
      let that = this;
      // 调起条码扫描
      uni.scanCode({
        onlyFromCamera: true,
        scanType: ["qrCode"],
        autoZoom: false,
        success: function (res) {
          console.log(`扫码结果：${JSON.stringify(res, null, 2)}`);
          that.parseQRCode(res);
        },
        fail: function (err) {
          console.log(`错误：${err}`);
        },
        complete: function (data) {
          console.log("complete", data);
        },
      });
    },
  },
};
</script>

<style lang="scss">
.container {
  display: flex;
  width: 100vw;
  min-height: 100vh;
  flex-direction: column;
  padding: 234rpx 32rpx 0;
  box-sizing: border-box;
  background: radial-gradient(
    128% 128% at 95% 0%,
    #d7ceff 0%,
    #bbe1ff 62%,
    #ffffff 95%
  );
  .list {
    // padding: 32rpx 24rpx;
    // box-sizing: border-box;
    margin-top: 24rpx;

    .list-item {
      padding: 24rpx 40rpx;
      box-sizing: border-box;
      border-radius: 20rpx;
      background: linear-gradient(
        180deg,
        rgba(255, 255, 255, 0.62) 64%,
        #ffffff 100%
      );
      box-shadow: 0rpx 0rpx 40rpx 0rpx rgba(0, 0, 0, 0.04);
      border: 2rpx solid #ffffff;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 24rpx;

      .left-image {
        flex-shrink: 0;
        border-radius: 12rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #ff4e04;
        font-weight: 900;
      }

      .right {
        .title {
          font-weight: 700;
          font-size: 36rpx;
          color: #000000;
          line-height: 36rpx;
          margin-bottom: 16rpx;
        }

        .date {
          font-weight: 400;
          font-size: 28rpx;
          color: #86909c;
          line-height: 40rpx;
        }
      }
    }
  }
}
</style>
